/**
 * 资产质量数据状态管理
 * 用于在不同组件间共享资产质量数据，避免重复请求
 */

import { ref, computed } from 'vue'

// 全局状态
const assetData = ref({})
const branchBadLoanData = ref([])
const industryBadLoanData = ref([])
const businessLineBadLoanData = ref([])
const topBadLoanCustomers = ref([])  // 前十大不良贷款户
const isLoading = ref(false)
const lastUpdateTime = ref(null)

/**
 * 使用资产质量数据存储
 */
export function useAssetQualityStore() {
  /**
   * 设置资产质量核心数据
   * @param {Object} data - 核心资产质量数据
   */
  const setAssetData = (data) => {
    assetData.value = data
    lastUpdateTime.value = Date.now()
    console.log('[资产质量存储] 核心数据已更新')
  }

  /**
   * 设置支行不良贷款数据
   * @param {Array} data - 支行数据
   */
  const setBranchData = (data) => {
    branchBadLoanData.value = data
    console.log('[资产质量存储] 支行数据已更新，共', data.length, '个支行')
  }

  /**
   * 设置行业不良贷款数据
   * @param {Array} data - 行业数据
   */
  const setIndustryData = (data) => {
    industryBadLoanData.value = data
    console.log('[资产质量存储] 行业数据已更新，共', data.length, '个行业')
  }

  /**
   * 设置业务条线数据
   * @param {Array} data - 业务条线数据
   */
  const setBusinessLineData = (data) => {
    businessLineBadLoanData.value = data
    console.log('[资产质量存储] 业务条线数据已更新，共', data.length, '条')
  }

  /**
   * 设置前十大不良贷款户数据
   * @param {Array} data - 前十大不良贷款户数据
   */
  const setTopBadLoanCustomers = (data) => {
    topBadLoanCustomers.value = data
    console.log('[资产质量存储] 前十大不良贷款户数据已更新，共', data.length, '户')
  }

  /**
   * 获取核心数据
   * @returns {Object} 核心资产质量数据
   */
  const getAssetData = () => {
    return assetData.value
  }

  /**
   * 获取支行数据
   * @returns {Array} 支行不良贷款数据
   */
  const getBranchData = () => {
    return branchBadLoanData.value
  }

  /**
   * 获取行业数据
   * @returns {Array} 行业不良贷款数据
   */
  const getIndustryData = () => {
    return industryBadLoanData.value
  }

  /**
   * 获取业务条线数据
   * @returns {Array} 业务条线数据
   */
  const getBusinessLineData = () => {
    return businessLineBadLoanData.value
  }

  /**
   * 获取前十大不良贷款户数据
   * @returns {Array} 前十大不良贷款户数据
   */
  const getTopBadLoanCustomers = () => {
    return topBadLoanCustomers.value
  }

  /**
   * 检查是否有数据
   * @returns {boolean}
   */
  const hasData = computed(() => {
    return Object.keys(assetData.value).length > 0 && 
           branchBadLoanData.value.length > 0
  })

  /**
   * 设置加载状态
   * @param {boolean} loading
   */
  const setLoading = (loading) => {
    isLoading.value = loading
  }

  /**
   * 清除所有数据
   */
  const clearData = () => {
    assetData.value = {}
    branchBadLoanData.value = []
    industryBadLoanData.value = []
    businessLineBadLoanData.value = []
    topBadLoanCustomers.value = []
    lastUpdateTime.value = null
    console.log('[资产质量存储] 所有数据已清除')
  }

  return {
    assetData,
    branchBadLoanData,
    industryBadLoanData,
    businessLineBadLoanData,
    topBadLoanCustomers,
    isLoading,
    lastUpdateTime,
    hasData,
    setAssetData,
    setBranchData,
    setIndustryData,
    setBusinessLineData,
    setTopBadLoanCustomers,
    getAssetData,
    getBranchData,
    getIndustryData,
    getBusinessLineData,
    getTopBadLoanCustomers,
    setLoading,
    clearData
  }
}

